/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dnl.proj.rami.license;

import dnl.jexem.cmd.CommandExecutionFactory;
import dnl.jexem.cmd.LocalCommandExcecution;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * @author User
 */
public class LicenseCreationPanel extends javax.swing.JPanel {

    LicenseManager licenseManager = new LicenseManager();

    /**
     * Creates new form LicenseCreationTab
     */
    public LicenseCreationPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        createLicenseJB = new javax.swing.JButton();

        jLabel1.setText("User Email:");

        createLicenseJB.setText("Create License");
        createLicenseJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createLicenseJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(createLicenseJB)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addGap(18, 18, 18)
                                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(59, 59, 59)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(71, 71, 71)
                                .addComponent(createLicenseJB)
                                .addContainerGap(139, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void createLicenseJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createLicenseJBActionPerformed
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String user = jTextField1.getText();
                String licenseContent = licenseManager.getLicenseFor(user);
                //String[] splits = StringUtils.split(user, "@");
                File currentDir = new File(".");
                File licenseFile = new File(currentDir, "rrr.license");
                try {
                    FileUtils.write(licenseFile, licenseContent);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                LocalCommandExcecution commandExecution = CommandExecutionFactory.createLocalCommandExecution("explorer.exe", currentDir.getAbsolutePath());
                commandExecution.executeCommand();
            }
        };
        SwingUtilities.invokeLater(r);
    }//GEN-LAST:event_createLicenseJBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createLicenseJB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
